package com.xbongbong.crm.handle.explain.impl;

import com.xbongbong.crm.handle.explain.CustomerExplainHandle;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.interfaces.explain.BusinessExplainHandle;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.service.toolbox.help.FundSetHelp;
import com.xbongbong.saas.service.toolbox.help.UserTeamHelp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/01/18 15:17
 */
@Service("customerExplainHandleImpl")
public class CustomerExplainHandleImpl implements BusinessExplainHandle, CustomerExplainHandle {
    @Resource
    private FundSetHelp fundSetHelp;
    @Resource
    private UserTeamHelp userTeamHelp;


    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
    }

    @Override
    public List<FieldAttrEntity> newHandleExplain(FormatExplainDTO formatExplainDTO) throws XbbException {
        UserVO loginUser = formatExplainDTO.getLoginUser();
        boolean adminTag = loginUser.isAdminOrBoss();
        boolean formShareTag = userTeamHelp.getFormShareTag(formatExplainDTO.getCorpid(), formatExplainDTO.getFormId(), formatExplainDTO.getBusinessType());
        List<FieldAttrEntity> explainList = formatExplainDTO.getExplainList();
        if (Objects.equals(formatExplainDTO.getSubBusinessType(), XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode())) {
            for (FieldAttrEntity fieldAttrEntity : explainList) {
                String attr = fieldAttrEntity.getAttr();
                if (Objects.equals(attr, CustomerManagementEnum.OWNER_ID.getAttr())) {
                    DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
                    defaultAttrPoJo.setDefaultType(null);
                    defaultAttrPoJo.setDefaultValue("");
                    fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
                    fieldAttrEntity.setEditable(0);
                } else if (Objects.equals(attr, CustomerManagementEnum.COUSERID.getAttr())) {
                    fieldAttrEntity.setEditable(0);
                }
            }
        }else if (!formShareTag) {
            //隔离模式
            if (adminTag) {
                explainList.forEach(fieldAttrEntity -> {
                    if (Objects.equals(fieldAttrEntity.getAttr(), CustomerManagementEnum.COUSERID.getAttr())) {
                        fieldAttrEntity.setVisible(BasicConstant.ZERO);
                    }
                });
            } else {
                explainList.forEach(fieldAttrEntity -> {
                    if (Objects.equals(fieldAttrEntity.getAttr(), CustomerManagementEnum.OWNER_ID.getAttr())) {
                        fieldAttrEntity.setMultiple(BasicConstant.ZERO);
                    }
                });
            }
        }
        fundSetHelp.formatPeriod(formatExplainDTO.getCorpid(),XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(),formatExplainDTO.getFormId(),explainList);
        return explainList;
    }


}
